package 剑指Offer1;

import java.util.PriorityQueue;

public class _37最小的k个数 {
    public static int[] getLeastNumbers(int[] arr, int k) {
        if(arr == null || arr.length == 0) return new int[0];
        //创建一个最大堆
        PriorityQueue<Integer> maxHeap = new PriorityQueue<>((o1,o2)->o2.compareTo(o1));
        for (int number : arr) {
            maxHeap.offer(number);
            if(maxHeap.size() > k){
                maxHeap.poll();
            }
        }
        int[] res = new int[maxHeap.size()];
        int i = 0;
        while(!maxHeap.isEmpty()){
            res[i++] = maxHeap.poll();
        }
        return res;
    }

    public static void main(String[] args) {
        getLeastNumbers(new int[]{3,2,1},2);
    }
}
